package cn.donting.jme;

import cn.donting.jme.editor.EditorUI;
import cn.donting.jme.event.EditorCloseEvent;
import cn.donting.jme.ioc.BeanInitialization;
import cn.donting.jme.log.java.JavaLoggerConfig;
import cn.donting.jme.project.Project;
import cn.donting.jme.project.gradle.GradleProject;
import cn.donting.jme.swing.JDTabbedPane;
import cn.donting.jme.swing.Tab;
import cn.donting.jme.swing.tab.ProjectTab;
import com.formdev.flatlaf.FlatDarculaLaf;
import com.formdev.flatlaf.extras.components.FlatTabbedPane;
import com.jme3.system.JmeContext;
import lombok.Getter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/***
 * -----------------------------------------------------------
 *    3      \        4                      |      1        |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *           \                               |               |
 *-------------------------------------------|               |
 *                 2                         |               |
 *                                           |               |
 *                                           |               |
 *                                           |               |
 *                                           |               |
 *                                           |               |
 * -----------------------------------------------------------
 */
public class EditorFrame extends JFrame implements EditorUI{
    private JPanel rootPanel;
    private JPanel panel1;
    private JPanel panel2;
    private JPanel panel3;
    private JPanel panel4;
    @Getter
    private JDTabbedPane jTabbedPane1;
    @Getter
    private JDTabbedPane jTabbedPane2;
    @Getter
    private JDTabbedPane jTabbedPane3;
    @Getter
    private JDTabbedPane jTabbedPane4;
    @Getter
    private JmeApplication jmeApplication;
    private Project project;

    public EditorFrame(Project project, JmeApplication jmeApplication) {
        setTitle("JME editor");
        EditorContent.editorFrame = this;
        this.project = project;
        this.jmeApplication = jmeApplication;
        pack();
        setSize(1200, 720);
        init();
    }

    private void init() {
        UIManager.put("TabbedPane.tabHeight", 20);
        rootPanel = new JPanel();
        rootPanel.setLayout(new BorderLayout());
        setContentPane(rootPanel);
        JSplitPane jSplitPane = new JSplitPane();
        JSplitPane jSplitPane1 = new JSplitPane();
        JSplitPane jSplitPane2 = new JSplitPane();
        panel1 = new JPanel();
        panel2 = new JPanel();

        panel3 = new JPanel();
        panel4 = new JPanel();

        panel1.setLayout(new BorderLayout());
        panel1.setMinimumSize(new Dimension(280, 100));
        panel2.setLayout(new BorderLayout());
        panel2.setMinimumSize(new Dimension(100, 100));
        panel3.setLayout(new BorderLayout());
        panel3.setMinimumSize(new Dimension(100, 100));
        panel4.setLayout(new BorderLayout());
        panel4.setMinimumSize(new Dimension(100, 100));


        jSplitPane.setLeftComponent(jSplitPane1);
        jSplitPane.setRightComponent(panel1);
        jSplitPane.setDividerLocation(860);

        jSplitPane1.setTopComponent(jSplitPane2);
        jSplitPane1.setBottomComponent(panel2);
        jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setDividerLocation(500);

        jSplitPane2.setLeftComponent(panel3);
        jSplitPane2.setRightComponent(panel4);
        jSplitPane2.setDividerLocation(200);




        rootPanel.add(jSplitPane, BorderLayout.CENTER);


        jTabbedPane1 = new JDTabbedPane();
        jTabbedPane2 = new JDTabbedPane();
        jTabbedPane3 = new JDTabbedPane();
        jTabbedPane4 = new JDTabbedPane();

        panel1.add(jTabbedPane1, BorderLayout.CENTER);
        panel2.add(jTabbedPane2, BorderLayout.CENTER);
        panel3.add(jTabbedPane3, BorderLayout.CENTER);
        panel4.add(jTabbedPane4, BorderLayout.CENTER);



        addTab(2, new ProjectTab(project));

        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        Frame frame = this;
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("windowClosing");
                EditorContent.postEvent(new EditorCloseEvent());
                jmeApplication.stop();
                frame.dispose();
            }
        });
    }

    @Override
    public void addTab(int index, Tab tab) {
        FlatTabbedPane jTabbedPane;
        switch (index) {
            case 1:
                jTabbedPane = jTabbedPane1;
                break;
            case 2:
                jTabbedPane = jTabbedPane2;
                break;
            case 3:
                jTabbedPane = jTabbedPane3;
                break;
            case 4:
                jTabbedPane = jTabbedPane4;
                break;
            default:
                throw new RuntimeException("index is 1-4");
        }
        jTabbedPane.addTab(tab.getTitle(), tab.getIcon(), tab, tab.getTip());
        jTabbedPane.setTabClosable(jTabbedPane.getTabCount() - 1, tab.isShowClosable());
        tab.setjTabbedPane(jTabbedPane);
    }

    @Override
    public <T extends Tab> T getTab(int index, Class<T> t) {
        JTabbedPane jTabbedPane = getjTabbedPane(index);
        int tabCount = jTabbedPane.getTabCount();
        for (int i = 0; i < tabCount; i++) {
            Component tabComponentAt = jTabbedPane.getComponentAt(i);
            if (tabComponentAt.getClass().equals(t)) {
                return (T) tabComponentAt;
            }
        }
        return null;
    }

    private JTabbedPane getjTabbedPane(int index) {
        JTabbedPane jTabbedPane;
        switch (index) {
            case 1:
                jTabbedPane = jTabbedPane1;
                break;
            case 2:
                jTabbedPane = jTabbedPane2;
                break;
            case 3:
                jTabbedPane = jTabbedPane3;
                break;
            case 4:
                jTabbedPane = jTabbedPane4;
                break;
            default:
                throw new RuntimeException("index is 1-4");
        }
        return jTabbedPane;
    }


    @Override
    public List<Tab> getTabs(int index) {
        List<Tab> tabs = new ArrayList<>();
        JTabbedPane jTabbedPane = getjTabbedPane(index);
        int tabCount = jTabbedPane.getTabCount();
        for (int i = 0; i < tabCount; i++) {
            Component tabComponentAt = jTabbedPane.getComponentAt(i);
            if (tabComponentAt instanceof Tab) {
                tabs.add((Tab) tabComponentAt);
            }
        }
        return tabs;
    }

    public static void main(String[] args) {
        FlatDarculaLaf.setup();
        //加载默认java  logger
        JavaLoggerConfig.load();

        GradleProject gradleProject = new GradleProject(new File("./jme-template"));
        JmeApplication jmeApplication = JmeApplication.create();
        EditorContent.initContent(gradleProject);
        EditorFrame editorFrame = new EditorFrame(gradleProject, jmeApplication);
        jmeApplication.start(JmeContext.Type.Canvas, true);

        editorFrame.setVisible(true);

    }

}
